const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0';
const app=getApp();
//获得全局js变量
var util = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        url: 'https://jc.kxjb1319.com/api/miaozhimini/weixin/fileUpload',
        prev_url: '',
        avatarUrl: defaultAvatarUrl,
        nickname: '',
        realname:'',
        imgSrc:'../../images/add_img.png'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
       var paycode = app.globalData.userinfo.payCode;
       if(paycode != '' && paycode != null){
        console.log(paycode);
        console.log(1);
        this.setData({
            imgSrc:app.globalData.userinfo.payCode
        });
       }
        this.setData({
            avatarUrl:app.globalData.userinfo.userAvatar,
            nickname:app.globalData.userinfo.userNickname,
            realname:app.globalData.userinfo.userRealname
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    chooseImage(e) {
        const type = e.currentTarget.dataset.type;
        const that = this;
        wx.chooseImage({
            count: 6,
            sourceType: ['album'],
            success(res) {
                const tempFilePaths = res.tempFilePaths;
                that.setData({
                    user_avatar: res.tempFilePaths[0]
                });
                wx.showLoading({
                    title: '上传中',
                });
                wx.uploadFile({
                    url: that.data.url,
                    filePath: res.tempFilePaths[0],
                    name: 'file',
                    formData: {},
                    success(uploadFileRes) {
                        wx.hideLoading();
                        const data = JSON.parse(uploadFileRes.data).data;
                        that.setData({
                            imgSrc: data
                        });
                    }
                });
            }
        });
    },
    bindinput: function (e) {
        //console.log(e);
        var value = e.detail.value;
        this.setData({
            realname: value,
        })
        //console.log(this.data);
    },
    onChooseAvatar(e) {
        const { avatarUrl } = e.detail 
        this.setData({
            avatarUrl,
        })
        var that=this;
        wx.uploadFile({
            url: app.data.rooturl + 'weixin/fileUpload',
            header:{token:wx.getStorageSync('token')},
            name: 'file',
            filePath: avatarUrl,
            formData:{
            },
            success:function(res){
                var data = JSON.parse(res.data);
                that.setData({
                    avatarUrl:data.data
                })
            }
        })
    }, 
    
    //增加微信用户
    saveUser: function () {
        var that=this;
        var postdata = {
            userAvatar: that.data.avatarUrl,
            userNickname:that.data.nickname,
            userRealname:that.data.realname,
            payCode:that.data.imgSrc,
            isLogin:1
        }
        util.httpRrquest(app.data.rooturl + 'weixin/saveUser', 'POST',postdata)
        .then(function (response) {
            var data = response.data.data;
            app.globalData.userinfo = data;
            app.globalData.userId = data.id;
            app.globalData.userPhone = data.userPhone;
            wx.showToast({
                title: '保存成功',
                icon: 'success',
                duration: 2000
            })
            setTimeout(() => {
                wx.reLaunch({
                    url: '/pages/index/index' 
                })
            }, 2000)
        }, function (error) {
            console.log(error);
        });
    },
    logout(){
        var that=this;
        var postdata = {
            logout:1
        }
        util.httpRrquest(app.data.rooturl + 'weixin/saveUser', 'POST',postdata)
        .then(function (response) {
            app.globalData.userinfo = [];
            app.globalData.userId = 0;
            app.globalData.userPhone = 0;
            wx.showToast({
                title: '退出成功',
                icon: 'success',
                duration: 2000
            })
            setTimeout(() => {
                wx.reLaunch({
                    url: '/pages/index/index' 
                })
            }, 2000)
        }, function (error) {
            console.log(error);
        });
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        var shareinfo = app.setShareMessage();
        console.log(shareinfo);
        return {
            title: shareinfo.title,
            path: shareinfo.path,
            imageUrl: shareinfo.imageUrl,
            success: (res) => {
                wx.showToast({
                    title: "分享成功",
                    icon: 'loading'
                })
            }
        }
    }
})